<?php

namespace App\Http\Middleware;

use Closure;
use Dcat\Admin\Admin;
use Illuminate\Http\Request;

class RegisterAdminNavbar
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (Admin::app()) {
            Admin::navbar(function (\Dcat\Admin\Layout\Navbar $navbar) {
                // 添加到右侧
                $currentSiteId = Admin::context()->get('current_site_id');
                if (!$currentSiteId) {
                    $currentSiteId = session('current_site_id', 0);
                }


                $navbar->left(\App\Admin\Components\SiteSelector::render($currentSiteId));

                // 或者添加到左侧
                // $navbar->left(...);
            });
        }

        return $next($request);
    }
}
